import {getUsername, getValueFromUrl} from "./utils.js"

function getNewsDetail(nid) {
    // 获得浏览器地址栏的参数
    $.ajax({
        url: "http://www.woniu-news.com:8082/newsDetail.php",
        type: "GET",
        xhrFields: {
            withCredentials: true
        },
        data: {
            nid: nid
        },
        dataType: "json",
        success: (resp) => {
            if (resp.code === 200) {
                if (resp.data.newImg) {
                    const img = `<img class="new_img" width="500" src="${resp.data.newImg}" alt="新闻图片">`
                    $(".news-img").html(img)
                }
                const context = `
                        <h1 class="title">${resp.data.title}</h1>
                        <p class="meta">发布作者：<span class="newauther">${resp.data.username}</span></p>
                        <p class="meta">发布时间：<span class="newtime">${resp.data.time}</span></p>
                        <p class="newcontent">${resp.data.context}</p>
                        `
                $(".news-detail").html(context)
            }
        },
        error: (resp) => {
            alert(resp.message)
        }
    })
}

function getReview(nid) {
    // 获取评论
    $.ajax({
        url: "http://www.woniu-news.com:8082/getReview.php",
        type: "GET",
        xhrFields: {
            withCredentials: true
        },
        data: {
            nid: nid
        },
        dataType: "json",
        success: (resp) => {
            let context = "";
            if (resp.code === 200) {
                for (let item of resp.data) {
                    context += `
                        <div class="comment">
                            <div class="comment-header">
                                <span class="comment-author">${item.username}</span> |
                                <span class="comment-date">${item.time}</span>
                            </div>
                            <hr>
                            <div class="comment-content">
                                ${item.context}
                            </div>
                        </div>`
                }
            } else if (resp.code === 202) {
                context = `暂无评论`
                $(".comment-article")
            } else if (resp.code === 204) {
                $(".comment-article").attr("hidden", true)
            }
            $(".comments").html(context)
        },
        error: (resp) => {
            alert(resp.message)
        }
    })
}

function postReview(nid) {
    // 获得浏览器cookie中的用户名
    let username = getUsername()

    $.ajax({
        url: "http://www.woniu-news.com:8082/postReview.php",
        type: "GET",
        xhrFields: {
            withCredentials: true
        },
        data: {
            nid,
            username,
            context: $("#comment").val()
        },
        dataType: "json",
        success: (resp) => {
            if (resp.code === 200) {
                alert(resp.message)
                $("#comment").val("")
                getReview(nid)
            } else {
                alert(resp.message)
            }
        },
        error: (resp) => {
            alert(resp.message)
        }
    })
}


let nid = getValueFromUrl("nid")
if (nid) {
    getNewsDetail(nid)
    getReview(nid)
} else {
    alert("请选择新闻")
    window.location.href = "index.html"
}
$(".post-review").click(() => {
    postReview(nid)
})